package com.dongdong.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.dongdong.mapper.TbSellerMapper;
import com.dongdong.model.TbSeller;
import com.dongdong.model.TbSellerExample;
import com.dongdong.service.SellerService;
import com.dongdong.utils.*;
import com.dongdong.vo.SellerVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private TbSellerMapper sellerMapper;

    @Override
    public TbSeller getSellerByName(String username) {
        return sellerMapper.selectByPrimaryKey(username);
    }

    @Override
    public SellerVO getSeller(HttpServletRequest request) {
        String token = request.getHeader("accessToken");
//        String token = request.getHeaders().getFirst("accessToken");
        String id = jwtUtil.parseJwt(token).getId();


        TbSeller tbSeller = sellerMapper.selectByPrimaryKey(id);
        SellerVO sellerVO = new SellerVO();
        BeanUtils.copyProperties(tbSeller,sellerVO);
        return sellerVO;
    }

    @Override
    public void updateSeller(SellerVO sellerVO) {
        TbSeller seller = new TbSeller();
        BeanUtils.copyProperties(sellerVO,seller);
        sellerMapper.updateByPrimaryKeySelective(seller);
    }

    @Override
    public Result updatePwd(String oldPwd, String newPwd, HttpServletRequest request) {
        String token = request.getHeader("accessToken");
        String id = jwtUtil.parseJwt(token).getId();
        TbSeller seller = sellerMapper.selectByPrimaryKey(id);
//        判断密码
        String password = ShiroUtils.encryptPassword("md5", oldPwd, seller.getSalt(), 7);
        if (!Objects.equals(seller.getPassword(),password)){
            return Result.DATA(ResultEnum.PWD_ERROR);
        }
//        添加新密码
        String newPassword = ShiroUtils.encryptPassword("md5", newPwd, seller.getSalt(), 7);
        seller.setPassword(newPassword);
        sellerMapper.updateByPrimaryKeySelective(seller);
        return Result.OK();
    }

    @Override
    public void addSeller(TbSeller seller) {
//        String jwt = jwtUtil.createJwt(seller.getSellerId(), JSONObject.toJSONString(seller), "sellerRole");
        String salt = ShiroUtils.generateSalt(8);
        String password = ShiroUtils.encryptPassword("md5", seller.getPassword(), salt, 7);
        seller.setPassword(password);
        seller.setSalt(salt);
        seller.setCreateTime(new Date());
        sellerMapper.insertSelective(seller);
    }

    @Override
    public PageResult<SellerVO> getSellerPage(PageBean<SellerVO> pageBean) {
        TbSellerExample example = new TbSellerExample();
        TbSellerExample.Criteria criteria = example.createCriteria();
        if (!Objects.equals(pageBean.getParams().getName(),"")){
            criteria.andNameLike("%"+pageBean.getParams().getName()+"%");
        }
        if (!Objects.equals(pageBean.getParams().getNickName(),"")){
            criteria.andNickNameLike("%"+pageBean.getParams().getNickName()+"%");
        }
        if (!Objects.equals(pageBean.getParams().getStatus(),"")){
            criteria.andStatusEqualTo(pageBean.getParams().getStatus());
        }

        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<TbSeller> tbSellers = sellerMapper.selectByExample(example);
        PageInfo<TbSeller> info = new PageInfo<>(tbSellers);
        List<SellerVO> voList = tbSellers.stream().map((x) -> {
            SellerVO vo = new SellerVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult<>(info.getPageNum(),info.getPageSize(),info.getTotal(),voList);
    }

    @Override
    public void updateStatus(String status, String sellerId) {
        TbSeller seller = sellerMapper.selectByPrimaryKey(sellerId);
        seller.setStatus(status);
        sellerMapper.updateByPrimaryKeySelective(seller);
    }


}
